package com.vitas.coin.ui.dialog

import android.content.Context
import android.content.Intent
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.appcompat.widget.AppCompatImageView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.snap.base.util.BaseUtil
import com.vitas.coin.R
import com.vitas.coin.data.bean.ChildrenVoiceBean
import com.vitas.coin.data.event.CollectEvent
import com.vitas.coin.manager.LocalMusicManager
import com.vitas.coin.resp.CollectDaoResp
import com.vitas.coin.resp.CollectResp
import com.vitas.coin.ui.act.PlayAct
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import razerdp.basepopup.BasePopupWindow

class CollectListDialog(
    private val context: Context,
    val action: (data: Int) -> Unit
) : BasePopupWindow(context) {


    private var rv: RecyclerView? = null
    private var tv_number: TextView? = null

    var dataList: ArrayList<ChildrenVoiceBean> = arrayListOf()
    lateinit var mAdapter: MusicCollectAdapter

    init {
        contentView = createPopupById(R.layout.layout_collect_list_dialog)
        setBackground(null)
        initRv()
        initEvent()
        refresh()

    }

    private fun refresh() {
        CoroutineScope(Dispatchers.Main).launch {

            CollectResp.update { mList ->
                dataList.clear()
                dataList.addAll(mList)
                mAdapter.notifyDataSetChanged()
                tv_number?.text = "我的收藏(" + dataList.size + ")"
            }
        }
    }


    private fun initEvent() {
        findViewById<View>(R.id.cl_bag).setOnClickListener(View.OnClickListener { v ->
            dismiss()
        })
    }


    private fun initRv() {
        rv = findViewById(R.id.mRecyclerView)
        tv_number = findViewById(R.id.tv_number)
        val lm = LinearLayoutManager(context)
        rv?.layoutManager = lm
        mAdapter = MusicCollectAdapter();
        rv?.adapter = mAdapter
    }

    fun showXY(view: View) {
        val location = IntArray(2)
        view.getLocationInWindow(location)
        showPopupWindow(location[0], -(location[1] + view.measuredHeight))
    }


    inner class MusicCollectAdapter : RecyclerView.Adapter<MusicCollectAdapter.ViewHolder>() {

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
            val view =
                LayoutInflater.from(parent.context)
                    .inflate(R.layout.item_collect_music, parent, false)
            return ViewHolder(view)
        }

        override fun onBindViewHolder(holder: ViewHolder, position: Int) {
            val mItemData: ChildrenVoiceBean = dataList[position];
            holder.tv_name.text = mItemData.name
            holder.tv_duration.text = LocalMusicManager.getInstance(holder.tv_duration.context)
                .duration(mItemData.timeLength)
            holder.tv_classify.text = mItemData.parentName
            holder.iv_cancel.setOnClickListener { v ->
                CoroutineScope(Dispatchers.IO).launch {
                    CollectDaoResp.delete(mItemData)
                }
                refresh()
                mItemData.isCollect = false
                BaseUtil.postEvent(CollectEvent(mItemData))
            }

            holder.itemView.setOnClickListener { v ->
                if (!TextUtils.isEmpty(mItemData.name) && !TextUtils.isEmpty(mItemData.voiceUrl)) {
                    var i = Intent(holder.itemView.context, PlayAct::class.java)
                    i.putExtra("index", holder.adapterPosition)
                    i.putParcelableArrayListExtra("list", getList())
                    holder.itemView.context.startActivity(i)
                }
            }
        }

        fun getList(): ArrayList<ChildrenVoiceBean> {
            return dataList
        }

        override fun getItemCount(): Int {
            return dataList.size
        }

        inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
            val tv_name: TextView = itemView.findViewById(R.id.tv_name)
            val tv_duration: TextView = itemView.findViewById(R.id.tv_duration)
            val tv_classify: TextView = itemView.findViewById(R.id.tv_classify)
            val iv_cancel: AppCompatImageView = itemView.findViewById(R.id.iv_cancel)
        }

    }

}